package components;

import java.util.HashMap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.actionbarsherlock.app.SherlockFragment;
import com.example.remotecontrol.R;

import controller.AlarmOptionFragment;
import controller.DeviceOptionFragment;
import controller.HomeFragment;
import controller.PasscodeFragment;
import controller.PhoneFragment;
import controller.SMSLogFragment;
import controller.SetPasscodeFragment;
import controller.SettingFragment;
import controller.TabNavigation;
import controller.ViewPagerFragment;

public class FragmentMediator {

	public final static int FRAGMENT_HOME = 1;
	public final static int FRAGMENT_SETTING = 2;
	public final static int FRAGMENT_PHONE = 3;
	public static final int FRAGMENT_PASSCODE = 4;
	public static final int FRAGMENT_SET_PASSCODE = 5;
	public static final int FRAGMENT_ALARM_OPTION = 6;
	public static final int FRAGMENT_DEVICE_OPTION = 7;
	public static final int FRAGMENT_VIEW_PAGER = 8;
	public static final int FRAGMENT_SMS_LOG_MANAGER = 9;
	
	private static HashMap<Integer, Fragment> listFragement = new HashMap<Integer, Fragment>();
	public static Fragment CreateFragment(int fragmentID) {
		Fragment f = null;
		
		if (listFragement.get(fragmentID) != null) {
			f = listFragement.get(listFragement);
		} else {
		switch (fragmentID) {
		case FRAGMENT_HOME:
			f = new HomeFragment ();
			break;
		case FRAGMENT_SETTING:
			f=  new SettingFragment();
			break;
		case FRAGMENT_PHONE:
			f = new PhoneFragment();
			break;
		case FRAGMENT_PASSCODE:
			f = new PasscodeFragment();
			break;
		case FRAGMENT_SET_PASSCODE:
			f = new SetPasscodeFragment();
			break;
		case FRAGMENT_ALARM_OPTION:
			f = new AlarmOptionFragment();
			break;
		case FRAGMENT_DEVICE_OPTION:
			f = new DeviceOptionFragment();
			break;
		case FRAGMENT_VIEW_PAGER:
			f = new ViewPagerFragment();
			break;
		case FRAGMENT_SMS_LOG_MANAGER:
			f = new SMSLogFragment();
			default:
				break;
		}
		
		//listFragement.put(fragmentID, f);
		}
		return f;

	}
	
	public static void transitView(Fragment from, Fragment to, Bundle map, boolean isAddToBackStack){
		if (from instanceof HomeFragment){
		} else if (from instanceof SettingFragment) {
			if (to instanceof PhoneFragment) {
				
			}
		} else {
	
		}
		
		FragmentActivity activity = null;
		if (from == null || from.getActivity() == null) {
			activity = (FragmentActivity) TabNavigation.RemoteControllerContext;
		} else {
			activity = from.getActivity();
		}
		android.support.v4.app.FragmentTransaction ft = activity.getSupportFragmentManager()
				.beginTransaction();
		
		if (isAddToBackStack) {
		ft.addToBackStack(null);
		}
		
		ft.replace(R.id.tab_main_content, to);

		ft.commit();
		
	}
	
	public static void backView(Fragment from){
		if (from instanceof HomeFragment){
		} else if (from instanceof SettingFragment) {
			
		} else {
	
		}
		
		from.getActivity().getSupportFragmentManager().popBackStack();
	}
	}